<?php

namespace App\Admin\Controllers;

use App\Models\SwtPledgesRule;
use App\Models\SwtPledge;
use Dcat\Admin\Form;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SwtPledgesRuleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SwtPledgesRule(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('pledge_id','项目');
            $grid->column('year_rate');
            $grid->column('lock_day');
            $grid->column('show_index') -> using(['否','是']) -> label(['danger', 'success']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->actions(
                function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                }
            );
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SwtPledgesRule(), function (Show $show) {
            $show->field('id');
            $show->field('pledge_id');
            $show->field('year_rate');
            $show->field('lock_day');
            $show->field('redeem_rule');
            $show->field('show_index');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SwtPledgesRule(), function (Form $form) {
            $form -> disableDeleteButton();
            $form -> disableViewButton();

            $form->display('id');
            $form->select('pledge_id','项目')
                ->options(SwtPledge::where('status', 1)->pluck('name', 'id'))
                ->required()
                ->placeholder('请选择项目');
            $form->rate('year_rate') -> required();

            $form->number('lock_day') -> required()->help('请填写天数');

            $form->switch('show_index');

            if($form -> isEditing()) {
                $form->table('redeem_rule', function (NestedForm $table) {
                    $table -> number('day','天数（大于）') -> required();
                    $table -> rate('rate','利率') -> required();
                });
            }
        });
    }
}
